<?php
namespace LANGUAGE_EXCHANGE;
/*
 * 基于搜狗的中文分词
 */
class Sougou extends Word_segmentation{
    
    /*
     * 如果没有安装扩展 那么就用api的形式 这里是API地址
     */
    const API_URL = 'http://www.sogou.com/labs/webservice/sogou_word_seg.php';
    public function __construct() {
        parent::__construct();
        if(class_exists('scws_new'))
        {
            $this->scws_obj = scws_new();
        }
    }
    /*
     * 处理分词
     */
    public function exec($text = false)
    {
        if(false === $text and false === $this->text)
        {
            return false;
        }
        if(false !== $text)
        {
            $this->set_text($text);
        }
        $post = "q={$this->text}&fmt=js";
        $ch = curl_init(self::API_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $data = curl_exec($ch);
        curl_close($ch);
        eval("\$return = json_decode('$data');");
        if(isset($return->status) and 'OK' == $return->status)
        {
            $this->return_text = (array)$return;
            return $this->return_text;
        }
        return false;
    }
    
    /*
     * 设置需要分词的中文
     * @param string word
     * @return void
     */
    public function set_text($text)
    {
        $this->text = urlencode($text);
    }
}